﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaMusaSoft.RPS.DataModel;
using System.Drawing.Printing;
using System.Drawing;
using System.Globalization;
using System.Threading.Tasks;

namespace BaMusaSoft.RPS.RPSServices
{
    public class PrintingContractService
    {
        PrintableContract contract;
        ContractMargins margins;
        /// <summary>
        /// Create new Printing Contract Service.
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="margins"></param>
        /// <exception cref="ArgumentNullException">
        /// Either the contract provided or the margins is null.
        /// </exception>
        public PrintingContractService(PrintableContract contract, ContractMargins margins)
        {
            if (contract == null) throw new ArgumentNullException("contract");
            if (margins == null) throw new ArgumentNullException("margins");
            this.contract = contract;
            this.margins = margins;

        }
        /// <summary>
        /// Print contract in the printer.
        /// </summary>
        public void PrintAsync()
        {
            Task task = Task.Factory.StartNew(StartPrintingJob);
        }
        private void StartPrintingJob()
        {
            PrintDocument p = new PrintDocument();

            p.PrintPage += new PrintPageEventHandler(p_PrintPage);
            p.QueryPageSettings += new QueryPageSettingsEventHandler(p_QueryPageSettings);

            p.Print();
        }
        void p_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (e.Graphics)
            {
                RectangleF priningArea;
                using (StringFormat sf = new StringFormat())
                {
                    sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                    using (Font f = new Font("Simplified Arabic", 14, FontStyle.Bold | FontStyle.Italic))
                    {
                        //CustomerName
                        priningArea = new RectangleF(margins.CustomerNameStartPoint.X, margins.CustomerNameStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.CustomerName,
                                               f, Brushes.Black, priningArea, sf);
                        //CustomerId
                        priningArea = new RectangleF(margins.CustomerIdStartPoint.X, margins.CustomerIdStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(
                            contract.CustomerId, f, Brushes.Black, priningArea, sf);
                        ////AgreedRent
                        priningArea = new RectangleF(margins.AgreedRentStartPoint.X, margins.AgreedRentStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.AgreedRent, f, Brushes.Black, priningArea, sf);

                        //SignDay
                        priningArea = new RectangleF(margins.SignDayStartPoint.X, margins.SignDayStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.SignDay, f, Brushes.Black, priningArea, sf);

                        //SignHijriDate
                        priningArea = new RectangleF(margins.SignHijriDateStartPoint.X, margins.SignHijriDateStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.SignHijriDate, f, Brushes.Black, priningArea, sf);

                        //SignGregDate
                        priningArea = new RectangleF(margins.SingGregDateStartPoint.X, margins.SingGregDateStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.SignGregDate, f, Brushes.Black, priningArea, sf);

                        //CustomerIdDate
                        priningArea = new RectangleF(margins.CustomerIdDateStartPoint.X, margins.CustomerIdDateStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.CustomerIdDate, f, Brushes.Black, priningArea, sf);

                        //CustomerIdIssue
                        priningArea = new RectangleF(margins.CustomerIdIssueStartPoint.X, margins.CustomerIdIssueStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.CustomerIdIssue, f, Brushes.Black, priningArea, sf);

                        //CustomerAddress
                        priningArea = new RectangleF(margins.CustomerAddressStartPoint.X, margins.CustomerAddressStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.CustomerAddress, f, Brushes.Black, priningArea, sf);

                        //PropertyNo
                        priningArea = new RectangleF(margins.PropertyNoStartPoint.X, margins.PropertyNoStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.PropertyNo, f, Brushes.Black, priningArea, sf);
                        //PropertyType
                        priningArea = new RectangleF(margins.PropertyTypeStartPoint.X, margins.PropertyTypeStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.PropertyType, f, Brushes.Black, priningArea, sf);
                        //PropertyLocation
                        priningArea = new RectangleF(margins.ProprtyLocationStartPoint.X, margins.ProprtyLocationStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.PropetyLocation, f, Brushes.Black, priningArea, sf);
                        //AgreedRentText
                        priningArea = new RectangleF(margins.AgreedRentTextStartPoint.X, margins.AgreedRentTextStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.AgreedRentText, f, Brushes.Black, priningArea, sf);
                        //AgreedDeposit
                        priningArea = new RectangleF(margins.AgreedDepositStartPoint.X, margins.AgreedDepositStartPoint.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.AgreedDeposit, f, Brushes.Black, priningArea, sf);
                        //AgreedDepositText
                        priningArea = new RectangleF(margins.AgreedDepositTextStartPoing.X, margins.AgreedDepositTextStartPoing.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.AgreedDepositText, f, Brushes.Black, priningArea, sf);
                        //ContractActivity
                        priningArea = new RectangleF(margins.ContractActivityStartPoing.X, margins.ContractActivityStartPoing.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.ContractActivity, f, Brushes.Black, priningArea, sf);
                        //ContractPeriod
                        priningArea = new RectangleF(margins.ContractPeriod.X, margins.ContractPeriod.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.ContractPeriod, f, Brushes.Black, priningArea, sf);
                        //StartDate
                        priningArea = new RectangleF(margins.StartDate.X, margins.StartDate.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.StartDate, f, Brushes.Black, priningArea, sf);
                        //EndDate
                        priningArea = new RectangleF(margins.EndDate.X, margins.EndDate.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.EndDate, f, Brushes.Black, priningArea, sf);
                        //ContractMaintinance
                        priningArea = new RectangleF(margins.AgreedMaintinance.X, margins.AgreedMaintinance.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                        e.Graphics.DrawString(contract.AgreedMaintinance, f, Brushes.Black, priningArea, sf);

                    }
                }
            }

        }
        private Font ScaleFont(Graphics g, Font font)
        {
            return new Font(
                font.FontFamily, font.SizeInPoints / 72f * g.DpiY, font.Style, GraphicsUnit.Pixel, font.GdiCharSet, font.GdiVerticalFont);
        }

        void p_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
        {
            e.PageSettings.Margins = new Margins(0, 0, 0, 0);
        }
        private float GetCurrentX(PrintPageEventArgs e, float pointOfXCordinate)
        {
            return (((e.PageSettings.PaperSize.Width - e.PageSettings.Margins.Right) / 100) - pointOfXCordinate);
        }


    }
}
